package com.mall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mall.bean.Goods;
import com.mall.bean.Keyword;
import com.mall.bean.Search;
import com.mall.bean.User;
import com.mall.service.IGoodsService;
import com.mall.service.IIdleService;
import com.mall.service.IKeywordService;
import com.mall.service.ISearchService;

@Controller
public class SearchController {

	@Resource
	private ISearchService searchService;

	@Resource
	private IKeywordService keywordService;

	@Resource
	private IGoodsService goodsService;

	@Resource
	private IIdleService idleService;

	//搜索商品页1
	@RequestMapping("search-1")
	@ResponseBody
	public Map<String, Object> search_1(@Param("content") String content, Model model, Search search, Keyword keyword) {

		Map<String, Object> res = new HashMap<String, Object>();
		String success = "恭喜您查询到";
		String nosearch = "未查询到";
		System.out.println(content);
		search.setGoods_type(content);
		search.setGoods_classify(content);
		search.setGoods_name(content);

		int in = searchService.isName(search);
		int it = searchService.isType(search);
		int ic = searchService.isClassify(search);

		keyword.setKeyword(content);
		int ik = keywordService.isKeyword(keyword);
		//根据类型搜索
		if (it >= 1) {
			res.put("success", success);
			return res;
		}
		//根据种类搜索
		else if (ic >= 1) {
			res.put("success", success);
			return res;
		}
		//更具内容搜索
		else if (in >= 1) {
			res.put("success", success);
			return res;
		} else {
			res.put("nosearch", nosearch);
			return res;
		}
	}

	//商品页1
	@RequestMapping("product-2")
	public String product_1(@Param("content") String content, Model model, Search search, Keyword keyword, HttpSession session) throws Exception {

		User user = (User) session.getAttribute("user");
		model.addAttribute("user", user);
		if (content != null) {
			search.setGoods_type(content);
			search.setGoods_classify(content);
			search.setGoods_name(content);


			int in = searchService.isName(search);
			int it = searchService.isType(search);
			int ic = searchService.isClassify(search);


			keyword.setKeyword(content);
			int ik = keywordService.isKeyword(keyword);
			System.out.println("ik:" + ik);
			if (ik == 0 && in >= 1 && it == 0 && ic == 0 && content != "") {
				keywordService.addKeyword(keyword);
			} else if (ik != 0) {
				int iN = keywordService.isNumber(keyword);
				int number = iN + 1;
				keyword.setNumber(number);
				keywordService.addNumber(keyword);
			}
			//根据类型搜索
			if (it >= 1) {
				List<Search> type = searchService.findAll_type(search);
				model.addAttribute("searchAll", type);
				model.addAttribute("content", content);
				return "product-2";
			}
			//根据种类搜索
			else if (ic >= 1) {
				List<Search> classify = searchService.findAll_classify(search);
				model.addAttribute("searchAll", classify);
				model.addAttribute("content", content);
				return "product-2";
			}
			//更具内容搜索
			else if (in >= 1) {
				List<Search> All = searchService.findAll(search);
				model.addAttribute("searchAll", All);
				model.addAttribute("content", content);
				return "product-2";
			}
		} else {
			List<Search> All = searchService.findAll(search);
			model.addAttribute("searchAll", All);
			model.addAttribute("content", "全部");
			return "product-2";
		}
		return "index-1";

	}

	//分类搜索
	@RequestMapping("classify_search")
	@ResponseBody
	public Map<String, Object> kind(@Param("requestPrice") String requestPrice, @Param("requestName") String requestName, @Param("classify") String classify, Model model, Goods goods) {

		Map<String, Object> res = new HashMap<String, Object>();

		System.out.println("1：" + requestPrice.split("~")[0]);
		System.out.println("2：" + requestPrice.split("~")[1]);
		if ("女装".equals(requestName)) {

			System.out.println("requestPrice：" + requestPrice);
			goods.setPrice_pre(requestPrice.split("~")[0]);
			goods.setPrice_next(requestPrice.split("~")[1]);
			goods.setGoods_type(requestName);
			goods.setGoods_classify(classify);
			List<Goods> searchAll = idleService.findAllClassify(goods);
			System.out.println(searchAll);
			res.put("searchAll", searchAll);
			return res;
		}
		if ("男装".equals(requestName)) {
			goods.setPrice_pre(requestPrice.split("~")[0]);
			goods.setPrice_next(requestPrice.split("~")[1]);
			goods.setGoods_type(requestName);
			goods.setGoods_classify(classify);
			List<Goods> searchAll = idleService.findAllClassify(goods);
			System.out.println(searchAll);
			res.put("searchAll", searchAll);
			return res;
		}

		if ("手机".equals(requestName)) {
			goods.setPrice_pre(requestPrice.split("~")[0]);
			goods.setPrice_next(requestPrice.split("~")[1]);
			goods.setGoods(requestName);
			goods.setGoods_classify(classify);
			List<Goods> searchAll = idleService.findAllClassify(goods);
			System.out.println(searchAll);
			res.put("searchAll", searchAll);
			return res;
		}

		if ("图书".equals(requestName)) {
			goods.setPrice_pre(requestPrice.split("~")[0]);
			goods.setPrice_next(requestPrice.split("~")[1]);
			goods.setGoods(requestName);
			goods.setGoods_classify(classify);
			List<Goods> searchAll = idleService.findAllClassify(goods);
			System.out.println(searchAll);
			res.put("searchAll", searchAll);
			return res;
		}

		return res;
	}

	//搜索商品页2
	@RequestMapping("product-1")
	public String product_2() {
		return "product-1";
	}


}
